package cn.star.jots.filmActor.service.impl;

import cn.star.jots.film.mapper.FilmMapper;
import cn.star.jots.filmActor.mapper.FAMapper;
import cn.star.jots.filmActor.pojo.dto.FilmActorSaveParam;
import cn.star.jots.filmActor.pojo.entity.FilmActor;
import cn.star.jots.filmActor.pojo.vo.FilmActorVO;
import cn.star.jots.filmActor.service.FAService;
import cn.star.jots.preview.mapper.PreviewMapper;
import cn.star.jots.tag.mapper.FilmTagMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class FAServiceImpl implements FAService {

    @Autowired
    FAMapper faMapper;
    @Autowired
    PreviewMapper previewMapper;
    @Autowired
    FilmMapper filmMapper;
    @Autowired
    FilmTagMapper filmTagMapper;

    @Override
    public List<FilmActorVO> selectByFilmId(Long filmId) {
        log.debug("根据电影id查询演员信息,参数:{}", filmId);
        return faMapper.selectByFilmId(filmId);
    }

    @Override
    public void save(FilmActorSaveParam filmActorSaveParam) {
        log.debug("保存电影演员信息,参数:{}", filmActorSaveParam);
        FilmActor filmActor = new FilmActor();
        BeanUtils.copyProperties(filmActorSaveParam, filmActor);
        if(filmActor.getId()==null){
            filmActor.setCreateTime(new Date());
            faMapper.insert(filmActor);
        } else {
            filmActor.setUpdateTime(new Date());
            faMapper.update(filmActor);
        }
    }

    @Override
    public List<FilmActorVO> selectById(Long id) {
        log.debug("根据电影id查询演员信息,参数:{}", id);
        return faMapper.selectById(id);
    }

    @Override
    public void delete(Long id) {
        log.debug("删除电影演员信息,参数:{}", id);
        faMapper.delete(id);
    }

    @Override
    public void deleteByFilmId(Long filmId) {
        log.debug("删除电影演员信息,参数:{}", filmId);
        faMapper.deleteByFilmId(filmId);
        previewMapper.deleteByFilmId(filmId);
        filmMapper.deleteFilm(filmId);
        filmTagMapper.deleteByFilmId(filmId);
    }
}
